﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        private Maticsoft.BLL.tree _bll;
      
        public Handler1()
        {
            _bll = new Maticsoft.BLL.tree();
            
        }
        public void ProcessRequest(HttpContext context)
        {
            XWPFDocument doc = new XWPFDocument();
            string type = context.Request["type"];

            string filepath = ""; //"/file/pdf/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
            string file = CreateFile(type, context, out filepath);






            string value = System.IO.File.ReadAllText(context.Server.MapPath("~/text.txt"), Encoding.Default);
            iTextSharp.text.Document pdf;
            pdf = new iTextSharp.text.Document();

            // 写入到客户端  
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            switch (type)
            {
                case "pdf":
                    
                    context.Response.ContentType = "application/pdf";
                    PdfHelper.ExportPDF(file, value, "");
                    break;
                case "word":

                    break;
                case "xls":
                    
                    NPOI.SS.UserModel.IWorkbook book= new NPOI.HSSF.UserModel.HSSFWorkbook();  
                    
                    NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("test_01");

                    var regex = new Regex("\\r\\n");
                    var dd= regex.Split(value);
                    var list = new List<string>();

                    int n = 0;
                    foreach (string item in dd)
                    {
                        NPOI.SS.UserModel.IRow row = sheet.CreateRow(n);
                        row.CreateCell(0).SetCellValue(item);

                        n += 1;
                    }


                    using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        ms.WriteTo(fs);
                    }

                    book.Write(ms);
                    break;
                case "ppt":
                    break;
                default:
                    break;
            }

            
            context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}."+type, DateTime.Now.ToString("yyyyMMddHHmmssfff")));
            context.Response.BinaryWrite(ms.ToArray());
            context.Response.TransmitFile(file);
            context.Response.End(); 
            ms.Close();
            ms.Dispose(); 
        }
        string CreateFile(string type,HttpContext context,out string filepath)
        { 
            filepath = "/file/" + type + "/" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + type;
            string file = context.Server.MapPath("~" + filepath);
            try
            {
                var fileinfo = new FileInfo(file);
                System.IO.Directory.CreateDirectory(fileinfo.DirectoryName);


            }
            catch (Exception ex)
            {

            }
          
            return file;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}